package com.mcode.blog.controller;

import com.mcode.blog.common.aop.Log;
import com.mcode.blog.common.cache.Cache;
import com.mcode.blog.service.ArticleService;
import com.mcode.blog.vo.ArticleVo;
import com.mcode.blog.vo.Result;
import com.mcode.blog.vo.params.ArticleParam;
import com.mcode.blog.vo.params.PageParams;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("articles")
public class ArticleController {

    @Resource
    private ArticleService articleService;


    /**
     * 文章列表
     * @param pageParams 分页参数
     * @return
     */
    // 加上此注解代表对接口记录日志
    @Log(module = "文章", operator = "获取文章列表")
    @PostMapping
    public Result listArticle(@Validated @RequestBody PageParams pageParams) {
        List<ArticleVo> list = articleService.listArticle(pageParams);
        return Result.success(list);
    }


    /**
     * 热门
     * @return
     */
    @PostMapping("/hot")
    @Cache(expire = 5 * 60 * 1000, name = "hot_article")
    public Result hotArticle() {
        int limit = 5;
        return Result.success(articleService.hotArticle(limit));
    }


    /**
     * 最新文章
     * @return
     */
    @PostMapping("/new")
    @Cache(expire = 5 * 60 * 1000, name = "new_article")
    public Result newArticle() {
        int limit = 5;
        return Result.success(articleService.newArticle(limit));
    }


    /**
     * 文章列表
     * @return
     */
    @PostMapping("/listArchives")
    public Result listArchives() {
        return Result.success(articleService.listArchives());
    }


    /**
     * 文章详情
     * @param articleId 文章id
     * @return
     */
    @PostMapping("/view/{id}")
    public Result articleView(@PathVariable("id") Long articleId) {
        ArticleVo articleVo = articleService.findArticleById(articleId);
        return Result.success(articleVo);
    }


    /**
     * 发布文章
     * @param articleParam 发布文章参数
     * @return
     */
    @Log(module = "文章", operator = "发布文章")
    @PostMapping("/publish")
    public Result publish(@RequestBody ArticleParam articleParam) {
        return Result.success(articleService.publish(articleParam));
    }


}
